﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace commonV2.controls
{
    public partial class baseListEdit2 : controls.baseUserControl
    {
        public baseListEdit2()
        {
            try
            {
                InitializeComponent();
                this.myCodeListLb.LoadData();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er); ;
            }
        }

        public delegate bool OnModifyItemList(object sender,StringCollection codes);
        public event OnModifyItemList myOnAddItemList = null;
        public event OnModifyItemList myOnRemoveItemList = null;

        private controls.baseListBox _myCodeListLb = null;
        protected controls.baseListBox myCodeListLb
        {
            get
            {
                if (_myCodeListLb == null || _myCodeListLb.IsDisposed)
                {
                    _myCodeListLb = CreateListObj();
                    this.Controls.Add(this._myCodeListLb);
                    _myCodeListLb.BringToFront();
                }
                return _myCodeListLb;
            }
        }
        protected virtual controls.baseListBox CreateListObj()
        {
            return new controls.baseListBox(); 
        }

        private forms.baseListSelection _myCodeSelectionForm = null;
        protected forms.baseListSelection myCodeSelectionForm
        {
            get
            {
                if (_myCodeSelectionForm == null || _myCodeSelectionForm.IsDisposed) _myCodeSelectionForm = CreateCodeSelectionForm();
                return _myCodeSelectionForm;
            }
        }
        protected virtual forms.baseListSelection CreateCodeSelectionForm()
        {
            SysLibs.ThrowException("CreateCodeSelectionForm() as not implemented");
            return null;
        }
        
        public string myItemString
        {
            get { return this.myCodeListLb.myItemString; }
            set { this.myCodeListLb.myItemString = value; }
        }
        public int maxLen
        {
            set { this.myCodeListLb.maxLen = value; }
        }
        public virtual bool DataValid(bool showMsg)
        {
            return this.myCodeListLb.DataValid(showMsg);
        }
        public virtual void LoadData()
        {
            this.myCodeListLb.LoadData();
        }
        public virtual void DataBining(string propertyName, BindingSource source, string fldName)
        {
            this.myCodeListLb.DataBindings.Clear();
            this.myCodeListLb.DataBindings.Add(new System.Windows.Forms.Binding(propertyName, source, fldName, true));
        }
        public virtual void LockEdit(bool state)
        {
            this.Enabled = !state;
            this.myCodeListLb.Enabled = !state;
            this.addBtn.Enabled = !state;
            this.removeBtn.Enabled = !state;
        }
        protected virtual string GetItemKey(object item)
        {
            return ((myTypes.ComboBoxItem)item).Value; 
        }
        
        private void form_Resize(object sender, EventArgs e)
        {
            try
            {
                this.myCodeListLb.Size = new Size(this.Width - addBtn.Width, this.Height);
                this.myCodeListLb.Location = new Point(0, 0);
                addBtn.Location = new Point(this.myCodeListLb.Width, 0);
                removeBtn.Location = new Point(this.myCodeListLb.Width, addBtn.Height);
            }
            catch (Exception er)
            {
                ErrorHandler(this, er); ;
            }         
        }
        private void removeBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string code;
                StringCollection removedCodes = new StringCollection();
                while (this.myCodeListLb.SelectedItems.Count > 0)
                {
                    code = GetItemKey(this.myCodeListLb.SelectedItems[0]);
                    removedCodes.Add(code);
                    this.myCodeListLb.Items.Remove(this.myCodeListLb.SelectedItems[0]);
                }
                if (myOnRemoveItemList != null)
                    myOnRemoveItemList(sender, removedCodes);
            }
            catch (Exception er)
            {
                ErrorHandler(this,er);
            }
        }
        private void addBtn_Click(object sender, EventArgs e)
        {
            try
            {
                myCodeSelectionForm.ShowForm();
                if (myCodeSelectionForm.mySelectedCodes == null) return;
                StringCollection list = this.myCodeListLb.myItemValues;
                StringCollection addedCodes = new StringCollection();
                for (int idx = 0; idx < myCodeSelectionForm.mySelectedCodes.Count; idx++)
                {
                    if (list.Contains(myCodeSelectionForm.mySelectedCodes[idx])) continue;
                    addedCodes.Add(myCodeSelectionForm.mySelectedCodes[idx]);
                    list.Add(myCodeSelectionForm.mySelectedCodes[idx]);
                }
                this.myCodeListLb.myItemValues = list;
                if (myOnAddItemList != null)
                {
                    myOnAddItemList(sender, addedCodes);
                }
            }
            catch (Exception er)
            {
                ErrorHandler(this,er);
            }
        }
    }
}
